<?php
header("Content-Type:text/html; charset=utf-8");
include_once('../inc/init.php');
include_once('../inc/cache.php');
include_once('inc/adminfun.php');
if(!isAdmin()){
	exit("请登录");
}

$action=strFilter($_GET['action']);
switch($action){
	case "savesignupinfo":
	case "savesignupinfo2":
	case "savesettings":
	case "savetime":
	case "saveseoinfo":
	case "saveverifyinfo":
	case "savebbs1":
	case "savebbs2":
	case "savebbs3":
	case "savebbs4":
	case "savebbs5":
	case "savebbs6":
	case "savereward":
	case "savemail":
	case "savesalt":
	case "saveapikey":
	case "saveurlrewriteinfo":
		$settings = $_POST['settings'];
		try{
			saveSettings($settings);
			writeSettingsCache();
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
		
	break;	

	case "getstat":
		$res='';
		$row=$db->row_select_one("topics", "", "count(0) as C");
		$res.=$row['C'].",";
		$row=$db->row_select_one("posts", "", "count(0) as C");
		$res.=$row['C'].",";
		$row=$db->row_select_one("users", "", "count(0) as C");
		$res.=$row['C'].",";
		echo($res);
	break;

	case "getforumstat":
		$topicsnumrows = $db->row_query("SELECT fid,count(0) AS c FROM `{$db->pre}topics` WHERE isrecycle=0 AND verifystate=0 GROUP BY fid"); 
		$postsnumrows = $db->row_query("SELECT fid,count(0) AS c FROM `{$db->pre}posts` WHERE verifystate=0 GROUP BY fid"); 
		$res='';
		foreach($topicsnumrows as $key=>$tc){
			$topicsnumrows[$key]=implode(":", $tc);
		}
		$res.=implode(",", $topicsnumrows);
		$res.="|";
		foreach($postsnumrows as $key=>$tc){
			$postsnumrows[$key]=implode(":", $tc);
		}
		$res.=implode(",", $postsnumrows);
		
		echo($res);
	break;

	case "savestat":
		$cache1 = $_POST['cache1'];
		$cache1['topicsnum']=numFilter($cache1['topicsnum']);
		$cache1['postsnum']=numFilter($cache1['postsnum']);
		$cache1['usersnum']=numFilter($cache1['usersnum']);
		$cache1['mostnum']=numFilter($cache1['mostnum']);
		$cache1['mostonlinenum']=numFilter($cache1['mostonlinenum']);
		$cache1['mostonlinetime'] = empty($cache1['mostonlinetime'])?0:strtotime($cache1['mostonlinetime'])-$cache_settings['timeoffset']*3600;
		$db->row_update("cache1",$cache1);
		echo("y");
	break;

	case "saveforumstat":
		$topicsnumrows = $_POST['topicsnum'];
		$postsnumrows = $_POST['postsnum'];
		foreach($topicsnumrows as $key=>$tnum){
			$forum['topicsnum']=$tnum;
			$forum['postsnum']=$postsnumrows[$key];
			$db->row_update("forums",$forum,"id={$key}");
		}
		echo("y");
	break;

	case "savecredits":
		try{
			for($i=0;$i<8;$i++){
				$id = numFilter($_POST["id_{$i}"]);
				$credits['isuse'] = strFilter($_POST["isuse_{$i}"]);
				$credits['creditsname'] = strFilter($_POST["creditsname_{$i}"]);
				$credits['postvar'] = strFilter($_POST["postvar_{$i}"]);
				$credits['replyvar'] = strFilter($_POST["replyvar_{$i}"]);
				$credits['goodvar'] = strFilter($_POST["goodvar_{$i}"]);
				$credits['uploadvar'] = strFilter($_POST["uploadvar_{$i}"]);
				$credits['downvar'] = strFilter($_POST["downvar_{$i}"]);
				$credits['votevar'] = strFilter($_POST["votevar_{$i}"]);
				$db->row_update("credits",$credits,"id={$id}");
			}
			writeCreditsCache();
			echo("y");
		}catch(Exception $e){
			echo($e);
		}
	break;

	case "savecreditsexp":
		$settings = $_POST['settings'];
		$exp=$settings['creditsexpression'];
		if(empty($exp)){
			exit("请填写完整公式");
		}
		$exp=str_replace(array('postsnum', 'credits1', 'credits2', 'credits3', 'credits4', 'credits5', 'credits6', 'credits7', 'credits8' ), array('10', '10', '20', '30', '40', '50', '60', '70', '80'), $exp);
		try{
			eval("\$r={$exp};");
		}catch(Exception $e){
			exit("公式错误，无法计算结果。");
		}
		try{
			if(!empty($r)){
				saveSettings($settings);
				writeSettingsCache();
				echo("y");
			}
		}catch(Exception $e){
			echo($e);
		}
	break;

	case "testcreditsexp":
		$settings = $_POST['settings'];
		$exp=$settings['creditsexpression'];
		if(empty($exp)){
			exit("0");
		}
		$exp=str_replace(array('postsnum', 'credits1', 'credits2', 'credits3', 'credits4', 'credits5', 'credits6', 'credits7', 'credits8' ), array('10', '10', '20', '30', '40', '50', '60', '70', '80'), $exp);
		try{
			eval("\$r={$exp};");
		}catch(Exception $e){
			exit("公式错误，无法计算结果。");
		}
		echo("succeed=".$r);
	break;

	case "testmail":
		require_once('../inc/email.php');
		$settings = $_POST['settings'];
		$testmail = $_POST['testmail'];
		$sendmailtype=numFilter($settings['mailsendtype']);
		$mailsubject = "6KBBS测试邮件 @ ".getDateStr(time(),false,false);//邮件主题
		$mailbody = "<h1>测试邮箱内容：<a href='http://www.6kbbs.com' target='_blank'>www.6kbbs.com</a></h1>";//邮件内容
		$sender = $testmail['sender'];
		$receiver = $testmail['receiver'];
		if(sendMail($sendmailtype, $receiver, $sender, $mailsubject, $mailbody)){
			echo("y");
		}
	break;

	case "rebuildcache":
		$cachetype=$_POST['cachetype'];
		if(empty($cachetype)){
			exit("请选择重新生成缓存的类别。");
		}
		if(substr($cachetype,0,4)=='all,'){
			$cachetype='all';
		}
		$types= explode(",",$cachetype);
		if($cachetype=='all'||in_array('ads',$types)){ writeAdsCache();}
		if($cachetype=='all'||in_array('announces',$types)){ writeAnnouncesCache();}
		if($cachetype=='all'||in_array('credits',$types)){ writeCreditsCache();}
		if($cachetype=='all'||in_array('forums',$types)){ writeForumsCache();}
		if($cachetype=='all'||in_array('groups',$types)){ writeGroupsCache();}
		if($cachetype=='all'||in_array('links',$types)){ writeLinksCache();}
		if($cachetype=='all'||in_array('settings',$types)){ writeSettingsCache();}
		if($cachetype=='all'||in_array('stamps',$types)){ writeStampsCache();}
		if($cachetype=='all'||in_array('templates',$types)){ writeTemplatesCache();}
		if($cachetype=='all'||in_array('plugins',$types)){ writePluginsCache();}
		echo("y");
	break;

	default:
		echo"No Such Action";
	break;
}
?>
